package com.hgj.common.ws.config;

import com.alibaba.fastjson.JSON;
import com.hgj.common.ws.server.HSocket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
//@EnableWebSocket
//@ConditionalOnProperty(prefix = "wsConfig",name = "enable",havingValue = "true")
public class RedisWebsocketConfig {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter();
        serverEndpointExporter.setAnnotatedEndpointClasses(HSocket.class);
        return serverEndpointExporter;
    }

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory factory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);

        // 订阅本节点的广播频道
        container.addMessageListener(messageListener(),
                new ChannelTopic("ws:broadcast:" + HSocket.getNodeId()));

        return container;
    }

    @Bean
    public MessageListenerAdapter messageListener() {
        return new MessageListenerAdapter(new BroadcastReceiver());
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        HSocket.setRedisTemplate(template); // 注入到WebSocket类
        return template;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        HSocket.setStrRedisTemplate(template); // 注入到WebSocket类
        return template;
    }


    //接收消息发送到本地会话
    public static class BroadcastReceiver implements MessageListener {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            String payload = new String(message.getBody());
            HSocket.BroadcastTask task = JSON.parseObject(payload,
                    HSocket.BroadcastTask.class);

            HSocket.sendToLocalSessions(task.sessionIds, task.message);
        }
    }
}
